﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace TAM_SPORT
{
    public partial class RProperties : TAM_SPORT.FormBase
    {
        ProductDao dao;
        public RProperties()
        {
            InitializeComponent();
            dao = new ProductDao();
            loadCbx();
            lblUnitName.Text = "";
            cbxPro.SelectedIndex = 0;
            init();
        }

        private void loadCbx()
        {
            DataTable table = dao.getProperties("");
            cbxPro.DataSource = table;
            DataRow row = table.NewRow();
            row["ID"] = -1;
            row["NAME"] = "Chọn dụng cụ";
            table.Rows.InsertAt(row, 0);
            cbxPro.DisplayMember = "NAME";
            cbxPro.ValueMember = "ID";
        }
        private void btnsave_Click(object sender, EventArgs e)
        {
            if (cbxPro.SelectedIndex >= 1 && nbQuantity.Value >=1)
            {
                if (dao.insertProperties(int.Parse(cbxPro.SelectedValue.ToString()), (int)nbQuantity.Value))
                {
                    Result r = new Result(UserConstant.SAVE_SUCCESSFUL);
                    r.ShowDialog();
                    r.Dispose();
                    init();
                }
                else
                {
                    Error er = new Error(UserConstant.SAVE_FAIL);
                    er.ShowDialog();
                    er.Dispose();
                }
            }
            else
            {
                Error er = new Error(UserConstant.INPUT_VALIDATE);
                er.ShowDialog();
                er.Dispose();
            }
        }

        private void init()
        {
            DataTable table = dao.getPropertiesInfor();
            dtUnit.Rows.Clear();
            cbxPro.SelectedIndex = 0;
            nbQuantity.Value = 1;
            lblUnitName.Text = "";
            if (table != null && table.Rows.Count >= 0)
            {
                foreach (DataRow row1 in table.Rows)
                {
                    int i = dtUnit.Rows.Add();
                    dtUnit.Rows[i].Cells[0].Value = i + 1;
                    dtUnit.Rows[i].Cells[1].Value = row1["NAME"].ToString();                    
                    dtUnit.Rows[i].Cells[2].Value = row1["PRODUCER"].ToString();
                    dtUnit.Rows[i].Cells[3].Value = double.Parse(row1["PRICE"].ToString()).ToString("#,###.##");
                    dtUnit.Rows[i].Cells[4].Value = row1["UNAME"].ToString();
                    dtUnit.Rows[i].Cells[5].Value = double.Parse(row1["QUANTITY"].ToString()).ToString("#,###.##");
                    dtUnit.Rows[i].Cells[6].Value = row1["ID"].ToString();
                    if (i % 2 == 1)
                    {
                        dtUnit.Rows[i].DefaultCellStyle.BackColor = Color.LightCyan;
                    }
                }
            }
        }
        private void cbxPro_SelectedValueChanged(object sender, EventArgs e)
        {
            if (cbxPro.SelectedIndex >= 1)
            {
                try
                {

                    string s = cbxPro.SelectedValue == null ? "" : dao.getUnitNameofProduct(int.Parse(cbxPro.SelectedValue.ToString()));
                    lblUnitName.Text = s;
                }
                catch
                {
                    lblUnitName.Text = "";
                }

            }
        }

        private void dtUnit_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == 7)
            {
                int id = int.Parse(dtUnit.Rows[e.RowIndex].Cells[6].Value.ToString());
                EditProperties er = new EditProperties(id);
                er.ShowDialog();
                init();
            }
            else if (e.RowIndex >= 0 && e.ColumnIndex == 8)
            {
                int id = int.Parse(dtUnit.Rows[e.RowIndex].Cells[6].Value.ToString());
                Confirm con = new Confirm("Bạn có chắc muốn xóa dữ liệu này?");
                con.ShowDialog();
                if (con.IsAccept)
                {
                    if (dao.deleteProperties(id))
                    {
                        Result r = new Result("Xóa thành công.");
                        r.ShowDialog();
                        r.Dispose();
                        init();
                    }
                    else
                    {
                        Error er = new Error("Xóa thất bại.");
                        er.ShowDialog();
                        er.Dispose();
                    }
                }
                con.Dispose();
            }
        }

        private void btnadd_Click(object sender, EventArgs e)
        {
            AddAssets ad = new AddAssets();
            ad.ShowDialog();
            loadCbx();
            ad.Dispose();
        }
    }
}
